package com.music.activity;

import java.util.ArrayList;

import com.music.R;
import com.music.adapter.NetMusicListAdapter;
import com.music.utilbean.SearchResult;
import com.music.utilbean.SearchUtil;
import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Tab3ActivityMusic extends MusicBaseActivity implements OnClickListener{
	private WebView webView;
	private EditText search_edit;
	private ImageView img_search;
	private ImageView loading_img;
	private int page=1;
	private  NetBDMusicFragment fragment;
	private   ListView netMusic_list;
	
	  @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.tab3);
	       webView=(WebView) findViewById(R.id.webview);
	      webView.loadUrl("http://music.baidu.com/");//file:///android_asset/baidu.html
	        WebSettings webSettings=webView.getSettings();
	       webSettings.setJavaScriptEnabled(true);
	       

	       webView.setWebViewClient(new WebViewClient() {
	       public boolean shouldOverrideUrlLoading(WebView view, String url)
	                               { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
	                                       view.loadUrl(url);
	                                       return true;
	                               }
	       });
	     fragment=new NetBDMusicFragment();
		     FragmentManager fragmentManager=getFragmentManager();
		     FragmentTransaction transaction=fragmentManager.beginTransaction();
		     transaction.add(R.id.fragment2,fragment);
		     transaction.commit();
	     search_edit=(EditText) findViewById(R.id.searchText);
	     img_search=(ImageView) findViewById(R.id.searchBtn);
	     img_search.setOnClickListener(this);
	     loading_img=(ImageView)findViewById(R.id.loading_img);
	  
	   
	    }
	  @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		   netMusic_list =  (ListView) fragment.getView().findViewById(R.id.net_listview);
	}
	  @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}
	  @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	@Override
	public void publish(int progress) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void change(int position) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.searchBtn:
			searchMusic();
			break;

		default:
			break;
		}
	}
	private void searchMusic() {
		SearchUtil.hideInputMethod(search_edit);//隐藏输入法
		String key=search_edit.getText().toString();
		if(TextUtils.isEmpty(key)){
			Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show();
			return;
		}
		
		loading_img.setVisibility(View.VISIBLE);
		SearchUtil.getInstance().setListener(new SearchUtil.OnSearchMusicListener(){

			@Override
			public void onSearchResult(final ArrayList<SearchResult> results) {
				if(fragment.netMusicListAdapter!=null){
				ArrayList<SearchResult>sr=fragment.netMusicListAdapter.getSearchResults();				
				sr.clear();
				sr.addAll(results);
				fragment.netMusicListAdapter.notifyDataSetChanged();
				loading_img.setVisibility(View.GONE);
				}else {				
				fragment.netMusicListAdapter=new NetMusicListAdapter(Tab3ActivityMusic.this, results);
				netMusic_list.setAdapter(fragment.netMusicListAdapter);
				loading_img.setVisibility(View.GONE);
				}
			}
			
		}).search(key, page);
		
	}
	    

}
